package com.cloud.cloudshop.shop.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cloud.cloudshop.client.StockServiceClient;
import com.cloud.cloudshop.client.UidServiceClient;
import com.cloud.cloudshop.shop.dao.GoodsDao;
import com.cloud.cloudshop.shop.service.GoodsCatesService;
import com.cloud.cloudshop.shop.service.GoodsService;
import com.cloud.cloudshop.util.PageUtils;
import com.cloud.cloudshop.util.Query;
import com.cloud.common.entity.GoodsCatesEntity;
import com.cloud.common.entity.GoodsEntity;
import com.cloud.common.entity.GoodsKuEntity;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Map;


@Service("goodsService")
public class GoodsServiceImpl extends ServiceImpl<GoodsDao, GoodsEntity> implements GoodsService {

    @Autowired
    private UidServiceClient uidServiceClient;

    @Autowired
    private GoodsCatesService goodsCatesService;

    @Autowired
    private StockServiceClient stockServiceClient;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<GoodsEntity> page = this.page(
                new Query<GoodsEntity>().getPage(params),
                new QueryWrapper<GoodsEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public PageUtils queryPage(Map<String, Object> params, Long cateId) {

        String key = (String) params.get("key");
        QueryWrapper<GoodsEntity> queryWrapper = new QueryWrapper<GoodsEntity>();
        if (cateId != 0) {
            queryWrapper.eq("cates_id", cateId);
        }
        if (StringUtils.isNoneEmpty(key)) {
            queryWrapper.and((obj) -> {
                obj.eq("keywords", key).or().eq("goods_no", key).or().like("title", key);
            });
        }

        IPage<GoodsEntity> page = this.page(
                new Query<GoodsEntity>().getPage(params),
                queryWrapper
        );
        return new PageUtils(page);
    }

    @Override
    @Transactional
    public void addGoods(GoodsEntity goods) {
        //生成商品编码
        goods.setId(uidServiceClient.UidGenerator());

        //判断是不是三级菜单，必须添加三级菜单
        GoodsCatesEntity goodsCates = goodsCatesService.getById(goods.getCatesId());
        if (goodsCates == null) {
            throw new RuntimeException("分类菜单不存在");
        }
        if (goodsCates.getLevel() != 3) {
            throw new RuntimeException("必须选择三级菜单");
        }
        this.save(goods);

        //添加库存记录
        GoodsKuEntity goodsKuEntity = new GoodsKuEntity();
        goodsKuEntity.setId(uidServiceClient.UidGenerator());
        goodsKuEntity.setGoodsId(goods.getId());
        goodsKuEntity.setStock(0);
        goodsKuEntity.setRemark(goods.getRemark());
        stockServiceClient.save(goodsKuEntity);
    }
}